home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / TUT1-9.ZIP / TUT6.TXT < prev    next >
Text File  |  1993-10-01  |  9KB  |  187 lines

  1.  
  2.                    ╒═══════════════════════════════╕
  3.                    │         W E L C O M E         │
  4.                    │  To the VGA Trainer Program   │ │
  5.                    │              By               │ │
  6.                    │      DENTHOR of ASPHYXIA      │ │ │
  7.                    ╘═══════════════════════════════╛ │ │
  8.                      ────────────────────────────────┘ │
  9.                        ────────────────────────────────┘
  10.  
  11.                            --==[ PART 6 ]==--
  12.  
  13.  
  14.  
  15. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  16. ■ Introduction
  17.  
  18. Hi there! I'm back, with the latest part in the series : Pregenerated
  19. arrays. This is a fairly simple concept that can treble the speed of
  20. your code, so have a look.
  21.  
  22. I still suggest that if you haven't got a copy of TEXTER that you get it.
  23. This is shareware, written by me, that allows you to grab fonts and use
  24. them in your own programs.
  25.  
  26. I downloaded the Friendly City BBS Demo, an intro for a PE BBS, written
  27. by a new group called DamnRite, with coder Brett Step. The music was
  28. excellent, written by Kon Wilms (If I'm not mistaken, he is an Amiga
  29. weenie ;-)). A very nice first production, and I can't wait to see more
  30. of their work. I will try con a local BBS to allow me to send Brett some
  31. fido-mail.
  32.  
  33. If you would like to contact me, or the team, there are many ways you
  34. can do it : 1) Write a message to Grant Smith in private mail here on
  35.                   the Mailbox BBS.
  36.             2) Write a message here in the Programming conference here
  37.                   on the Mailbox (Preferred if you have a general
  38.                   programming query or problem others would benefit from)
  39.             3) Write to ASPHYXIA on the ASPHYXIA BBS.
  40.             4) Write to Denthor, Eze or Livewire on Connectix.
  41.             5) Write to :  Grant Smith
  42.                            P.O.Box 270 Kloof
  43.                            3640
  44.                            Natal
  45.             6) Call me (Grant Smith) at (031) 73 2129 (leave a message if you
  46.                   call during varsity)
  47.  
  48. NB : If you are a representative of a company or BBS, and want ASPHYXIA
  49.        to do you a demo, leave mail to me; we can discuss it.
  50. NNB : If you have done/attempted a demo, SEND IT TO ME! We are feeling
  51.         quite lonely and want to meet/help out/exchange code with other demo
  52.         groups. What do you have to lose? Leave a message here and we can work
  53.         out how to transfer it. We really want to hear from you!
  54.  
  55.  
  56. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  57. ■  Why do I need a lookup table? What is it?
  58.  
  59. A lookup table is an imaginary table in memory where you look up the
  60. answers to certain mathematical equations instead of recalculating them
  61. each time. This may speed things up considerably. Please note that a
  62. lookup table is sometimes referred to as a pregenerated array.
  63.  
  64. One way of looking at a lookup table is as follows : Let us say that for
  65. some obscure reason you need to calculate a lot of multiplications (eg.
  66. 5*5 , 7*4 , 9*2 etc.). Instead of actually doing a slow multiply each
  67. time, you can generate a kind of bonds table, as seen below :
  68.  
  69.  
  70. ╔═╤═╦═══════╤══════╤══════╤══════╤══════╤══════╤══════╤══════╤══════╗
  71. ╟─┼─╢   1   │  2   │  3   │  4   │  5   │  6   │  7   │  8   │  9   ║
  72. ╟─┴─╫═══════╪══════╪══════╪══════╪══════╪══════╪══════╪══════╪══════╡
  73. ║ 1 ║   1   │  2   │  3   │  4   │  5   │  6   │  7   │  8   │  9   │
  74. ╟───╫───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤
  75. ║ 2 ║   2   │  4   │  6   │  8   │  10  │  12  │  14  │  16  │  18  │
  76. ╟───╫───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤
  77. ║ 3 ║   3   │  6   │  9   │  12  │  15  │  18  │  21  │  24  │  27  │
  78. ╟───╫───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤
  79. ║ 4 ║   4   │  8   │  12  │  16  │  20  │  24  │  28  │  32  │  36  │
  80. ╟───╫───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤
  81. ║ 5 ║   5   │  10  │  15  │  20  │  25  │  30  │  35  │  40  │  45  │
  82. ╟───╫───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤
  83. ║ 6 ║   6   │  12  │  18  │  24  │  30  │  36  │  42  │  48  │  54  │
  84. ╟───╫───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤
  85. ║ 7 ║   7   │  14  │  21  │  28  │  35  │  42  │  49  │  56  │  63  │
  86. ╟───╫───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤
  87. ║ 8 ║   8   │  16  │  24  │  32  │  40  │  48  │  56  │  64  │  72  │
  88. ╟───╫───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤
  89. ║ 9 ║   9   │  18  │  27  │  36  │  45  │  54  │  63  │  72  │  81  │
  90. ╚═══╩───────┴──────┴──────┴──────┴──────┴──────┴──────┴──────┴──────┘
  91.  
  92. This means that instead of calculating 9*4, you just find the 9 on the
  93. top and the 4 on the side, and the resulting number is the answer.  This
  94. type of table is very useful when the equations are very long to do.
  95.  
  96. The example I am going to use for this part is that of circles. Cast
  97. your minds back to Part 3 on lines and circles. The circle section took
  98. quite a while to finish drawing, mainly because I had to calculate the
  99. SIN and COS for EVERY SINGLE POINT. Calculating SIN and COS is obviously
  100. very slow, and that was reflected in the speed of the section.
  101.  
  102.  
  103.  
  104. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  105. ■  How do I generate a lookup table?
  106.  
  107. This is very simple. In my example, I am drawing a circle. A circle has
  108. 360 degrees, but for greater accuracy, to draw my circle I will start
  109. with zero and increase my degrees by 0.4. This means that in each circle
  110. there need to be 8000 SINs and COSes (360/0.4=8000). Putting these into
  111. the base 64k that Pascal allocates for normal variables is obviously not
  112. a happening thing, so we define them as pointers in the following
  113. manner: 
  114.         TYPE   table = Array [1..8000] of real;
  115.  
  116.         VAR    sintbl : ^table;
  117.                costbl : ^table;
  118.  
  119. Then in the program we get the memory for these two pointers. Asphyxia 
  120. was originally thinking of calling itself Creative Reboot Inc., mainly 
  121. because we always forgot to get the necessary memory for our pointers.
  122. (Though a bit of creative assembly coding also contributed to this. We
  123. wound up rating our reboots on a scale of 1 to 10 ;-)). The next obvious
  124. step is to place our necessary answers into our lookup tables.  This can
  125. take a bit of time, so in a demo, you would do it in the very beginning
  126. (people just think it's slow disk access or something), or after you
  127. have shown a picture (while the viewer is admiring it, you are
  128. calculating pi to its 37th degree in the background ;-)) Another way of
  129. doing it is, after calculating it once, you save it to a file which you
  130. then load into the variable at the beginning of the program. Anyway,
  131. this is how we will calculate the table for our circle :
  132.  
  133.     Procedure Setup;
  134.     VAR deg:real;
  135.     BEGIN
  136.       deg:=0;
  137.       for loop1:=1 to 8000 do BEGIN
  138.         deg:=deg+0.4;
  139.         costbl^[loop1]:=cos (rad(deg));
  140.         sintbl^[loop1]:=sin (rad(deg));
  141.       END;
  142.     END;
  143.  
  144. This will calculate the needed 16000 reals and place them into our two
  145. variables. The amount of time this takes is dependant on your computer.
  146.  
  147.  
  148. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  149. ■  How do I use a lookup table?
  150.  
  151. This is very easy. In your program, wherever you put
  152.                cos (rad(deg)),
  153. you just replace it with :
  154.                costbl^[deg]
  155.  
  156. Easy, no? Note that the new "deg" variable is now an integer, always
  157. between 1 and 8000.
  158.  
  159.  
  160. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  161. ■ Where else do I use lookup tables?
  162.  
  163. Lookup tables may be used in many different ways. For example, when
  164. working out 3-dimensional objects, sin and cos are needed often, and are
  165. best put in a lookup table. In a game, you may pregen the course an
  166. enemy may take when attacking. Even saving a picture (for example, a
  167. plasma screen) after generating it, then loading it up later is a form
  168. of pregeneration.
  169.  
  170. When you feel that your program is going much too slow, your problems
  171. may be totally sorted out by using a table. Or, maybe not. ;-)
  172.  
  173.  
  174. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  175. ■  In closing
  176.  
  177. As you have seen above, lookup tables aren't all that exciting, but they
  178. are useful and you need to know how to use them. The attached sample
  179. program will demonstrate just how big a difference they can make.
  180.  
  181. Keep on coding, and if you finish anything, let me know about it! I
  182. never get any mail, so all mail is greatly appreciated ;-)
  183.  
  184. Sorry, no quote today, it's hot and I'm tired. Maybe next time ;-)
  185.  
  186.   - Denthor
  187.